<?php
class Admin_Models_UserModel extends Zend_Db_Table {
	protected $_name = "user";
	
	function selectdatabyday($productid,$time){
	    $db = Zend_Db_Table_Abstract::getDefaultAdapter();
	    $data = array();
	    $data_day_value = array();
	    foreach ($time as $day){
	        $mySql = $db->select()->from('userdata',array('Total'))
	    				->where('ProductID = '.$productid['ProductID'])
	    				->where("`Day` BETWEEN '".$day." 00:00:00' AND '".$day." 23:59:59'");
	        $result = $db->fetchRow($mySql);
	        $data_day_value[] = intval($result['Total']);
	    					 
	    }
	    $data['name'] = $productid['Title'];
	    $data['data'] = $data_day_value;
	    return  $data;
	} 
	
	function selectdatabyday2($productid,$time){
		$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		$data = array();
		$data_day_value = array();
		foreach ($time as $day){
			$mySql = $db->select()->from('userdata',array('Total'))
			->where('ProductID = '.$productid['ProductID'])
			->where("`Day` BETWEEN '".$day." 00:00:00' AND '".$day." 23:59:59'");
			$result = $db->fetchRow($mySql);
			$data_day_value[] = intval($result['Total']);
				
		}
		$data['name'] = 'Khác';
		$data['data'] = $data_day_value;
		return  $data;
	}
	
	function loadtopgamebytime($day){
	    $db = Zend_Db_Table_Abstract::getDefaultAdapter();
	    $mySql  = $db->select()->from('userdata',array('ProductID','Title'))
	    		 	 ->where("`Day` BETWEEN '".$day." 00:00:00' AND '".$day." 23:59:59'")
	    		     ->order('Total DESC');
	    
	    return $db->fetchAll($mySql);
	}
	function countUserRegByDay(){
		$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		
		$mySql = $db->select()
					->from(array('u'	=> $this->_name),array('user'	=> 'count(*)','day'	=> 'unix_timestamp(Date(u.create_time))'))
					->group('day')
					->order('day ASC');
		$result = $db->fetchAll($mySql);
		return $result;
	}
    function loadbyday($data){
        $db =  Zend_Db_Table_Abstract::getDefaultAdapter();
        $data_all = array();
        foreach ($data as $day){
            $mySql = $db->select()
            ->from('userdata')
            ->where("`Day` BETWEEN '".$day." 00:00:00' AND '".$day." 23:59:59'")
            ->group('ProductID')
            ->order('Total');
            $data = $db->fetchAll($mySql);
            
        }
    }
	
	function countUserByDay(){
	    
	    $db = Zend_Db_Table_Abstract::getDefaultAdapter();
	    
	    $mySql = $db->select()
	    ->from('userdata')
	    ->where("`Day` BETWEEN '".date('Y-m-d')." 00:00:00' AND '".date('Y-m-d')." 23:59:59'")
	    ->group('ProductID')
	    ->order('Total');
// 	    		echo $mySql;
// 	    		die;
	    $result = $db->fetchAll($mySql);
	    return $result;
// 	    Zend_Debug::dump($result);
// 	    die;
	}
	function countUserRegByDayWithProduct(){
		$db = Zend_Db_Table_Abstract::getDefaultAdapter();
	
		$mySql = $db->select()
		->from(array('u'	=> $this->_name),array('user'	=> 'count(*)','day'	=> 'unix_timestamp(Date(u.create_time))'))
		->join(array('up'=>'userproduct'), 'u.UserID = up.UserID',array('ProductID'))
		->where("u.create_time BETWEEN '".date('Y-m-d')." 00:00:00' AND '".date('Y-m-d')." 23:59:59' " ) 
		->group('ProductID')
		->order('user DESC');
// 		echo $mySql;
// 		die;	
		$result = $db->fetchAll($mySql);
		
		return $result;
	}


	
	
	function rangeMonth($datestr) {
		date_default_timezone_set(date_default_timezone_get());
		$dt = strtotime($datestr);
		$res['start'] = date('Y-m-d', strtotime('first day of this month', $dt));
		$res['end'] = date('Y-m-d', strtotime('last day of this month', $dt));
		return $res;
	}
	
	function rangeWeek($datestr) {
		date_default_timezone_set(date_default_timezone_get());
		$dt = strtotime($datestr);
		$res['start'] = date('N', $dt)==1 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('last monday', $dt));
		$res['end'] = date('N', $dt)==7 ? date('Y-m-d', $dt) : date('Y-m-d', strtotime('next sunday', $dt));
		return $res;
	}
	function countUserRegByDayBySort($data){
	  
	    if($this->_arrParam['time_search']=='0'){
	    	$dk3= "u.create_time BETWEEN '".$data['Starttime']."' AND '".$data['Endtime']."'"  ;
	    }else if($data['time_search']=='1'){
	    	$dk3= "u.create_time BETWEEN '".date('Y-m-d')." 00:00:00' AND '".date('Y-m-d')." 23:59:59'"  ; 
	    }else if($data['time_search']=='2'){
	    	$data_week = $this->rangeWeek(date('Y-m-d'));
	    	$dk3= "u.create_time BETWEEN '".$data_week['start']."' AND '".$data_week['end']."'"  ;
	    }else if($data['time_search']=='3'){
	    	$data_month = $this->rangeMonth(date('Y-m-d'));
	    	$dk3= "u.create_time BETWEEN '".$data_month['start']."' AND '".$data_month['end']."'"  ;
	    }else{
	    	$dk3="";
	    }
		$db = Zend_Db_Table_Abstract::getDefaultAdapter();
		
		$mySql = $db->select()
		->from(array('u'	=> $this->_name),array('user'	=> 'count(*)','day'	=> 'unix_timestamp(Date(u.create_time))'))
		
		->group('day')
		->order('day ASC');
		if($data['ProductID']!="0") $mySql->join(array('up'=>'userproduct'), 'u.UserID = up.UserID',array('ProductID'))		
		->where('up.ProductID = '.$data['ProductID']);
		if($dk3!="") $mySql->where($dk3);

		$result = $db->fetchAll($mySql);
	
		return $result;
	}
	function loadlistgame(){
         $db = Zend_Db_Table::getDefaultAdapter();
       $sql = $db->select()->from('product',array('ProductID','Title'))->where('status = 1');
       $result = $db->fetchAll($sql);
       return $result;        
    }
   
    function countUserRegByDayWithProductByOneDay($data){
    	//  Zend_Debug::dump($data);
		$dk3 = "u.create_time BETWEEN '".$data." 00:00:00' AND '".$data." 23:59:59'";
    	$db = Zend_Db_Table_Abstract::getDefaultAdapter();
    
    	$mySql = $db->select()
    	->from(array('u'	=> $this->_name),array('user'	=> 'count(*)','day'	=> 'unix_timestamp(Date(u.create_time))'))
    	->join(array('up'=>'userproduct'), 'u.UserID = up.UserID',array('ProductID'))
    	->group('ProductID')
    	->order('user DESC');
    	//->limit($data['limit']);
    	if($dk3!="") $mySql->where($dk3);
    
//     	//echo $dk3;
//     			echo $mySql;
//     			die;
    	$result = $db->fetchAll($mySql);
    	// 		Zend_Debug::dump($result);
    	// 		die;

    	return $result;
    }
   
   // function loaduser
    
}